package com.boot.controller;


import com.boot.entity.*;
import com.boot.entity.Product;
import com.boot.entity.R;
import com.boot.exception.ServiceException;
import com.boot.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * 商品表(Product)表控制层
 *
 * @author makejava
 * @since 2023-07-20 18:48:26
 */
@RestController
@RequestMapping("/product")
@CrossOrigin
public class ProductController  {
    /**
     * 服务对象
     */
    @Autowired
    private ProductService productService;

    /**
     * 管理员新增商品
     * @param p
     * @return
     */
    @RequestMapping("/insert")
    public R insertProduct(Product p){

        Boolean b = productService.insertProduct(p);
        if (b){
            return R.success(null);
        }else
            throw new ServiceException(401,"添加失败");
    }

    /**
     * 管理员修改商品上架状态
     * @param pid
     * @param status
     * @return
     */
    @RequestMapping("/updateStatus")
    public R updateStatus(Integer pid ,Integer status){

        Boolean b = productService.updateStatus(pid ,status);
        if(b){
            return R.success(null);
        }else throw new ServiceException(401,"修改上架状态失败");
    }

    /**
     * 管理员端展示所有商品
     * @return
     */
    @RequestMapping("/allList")
    public R showAll(){
        List<Product> list = productService.list();
        return R.success(list);
    }

    /**
     * 用户端展示所有上架商品
     * @return
     */
    @RequestMapping("/list")
    public R show(Integer pid){
        List<Product> list = productService.queryOneOrAllProduct(pid);
        return R.success(list);
    }

}

